/*
    ContactMod - Argentina Phone Number Fixer for J2ME
    Copyright (C) 2012 Gabriel Ernesto Cabral

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Contact: baigosoft@hotmail.com
*/
 
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
//import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
import javax.microedition.pim.*;
import java.util.Enumeration;
import java.util.Calendar;
import java.util.Date;

public class ContactMod extends MIDlet implements CommandListener 
{
	
private Form mForm;
private Command mExitCommand;
StringItem salidaSI;
String salida;

 
    public ContactMod() 
    {
        
    }
 
    public void startApp() 
    {


        boolean isAPIAvailable = false;
	mForm = new Form("ContactMod");
	salida="Iniciando...\n";
	salidaSI=new StringItem("ContactMod",salida);
        mForm.append(salidaSI);
        mExitCommand = new Command("Exit", Command.EXIT, 0);

    mForm.addCommand(mExitCommand);
    mForm.setCommandListener(this);
    Display.getDisplay(this).setCurrent(mForm);

        if (System.getProperty("microedition.io.file.FileConnection.version") != null)
        {
		isAPIAvailable = true;
		salida=salida+"-fileconnector disponible\n";
		//mForm.append(new StringItem(null, "Fileconnector disponible!"));

		retrieveContacts();
            
        }
        else
        {
            StringBuffer splashText = new StringBuffer
            (getAppProperty("MIDlet-Name")).append("\n").append(
            getAppProperty("MIDlet-Vendor")).
            append(isAPIAvailable?"":"\nFileConnection API not available");
            Alert splashScreen = new Alert(null,splashText.toString(),
            null,AlertType.INFO);
            splashScreen.setTimeout(3000);
            Display.getDisplay(this).setCurrent(splashScreen);
        }
    
    }
 
    public void pauseApp() {}
 
    public void destroyApp(boolean cond) 
    {
        notifyDestroyed();
    }
 
    public void commandAction(Command c, Displayable d) 
    {

	if (c == mExitCommand) {
	      destroyApp(true);
	      notifyDestroyed();
	}
        
    }
 

public void retrieveContacts() {

	String[] clists;

	System.out.println("buscando contactos");
	salida=salida+"-obteniendo listas de contactos\n";
	salidaSI.setText(salida);
        PIM pim = PIM.getInstance();
        ContactList cl = null;
        Contact search_template = null;
        Enumeration s_contacts = null;
	
	clists=PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
	salida=salida+"-se encontraron "+clists.length+" lista(s) de contactos\n";
	for(int x=0;x<clists.length;x++){
		salida=salida+"* recorriendo lista "+x+": "+clists[x]+"\n";		

		try {

		    cl = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, 
		          PIM.READ_WRITE,clists[x]);
		} catch (PIMException e) {
			salida=salida+"-¡error accediendo a la lista!\n";
			salidaSI.setText(salida);
			return;
		} catch (SecurityException e) {
			salida=salida+"-no se puede acceder a la lista: permiso denegado.\n";
			salidaSI.setText(salida);
		    	return;
		}

	Enumeration en;
	try {
	en = cl.items();


		while(en.hasMoreElements()) {
		    Contact contact = (Contact) en.nextElement();

			int numtel=contact.countValues(Contact.TEL);
			boolean change=false;
			String contactTel;
			int contactAtt=0;
			String carac="";
			String telefono="";
			String nuevotel="";

			for(int i=0;i<numtel;i++){

				contactTel = contact.getString(Contact.TEL, i);
				System.out.println("telefono completo: "+contactTel); //telefono

				salida=salida+contactTel+"->";
				salidaSI.setText(salida);

				contactAtt = contact.getAttributes(Contact.TEL, i);
				System.out.println("atributos: "+contactAtt); //atributos del telefono
	
				telefono=contactTel.substring(contactTel.length()-6,contactTel.length());
				System.out.println("telefono: "+telefono); //característica

				String temp="";
				System.out.println("longitud del numero: "+contactTel.length()); //característica
				int length=contactTel.length()-6;
				change=false;
				for(int j=0;j<length;j++){
					temp=contactTel.substring(j,j+4);


					if(temp.equals("3752")){
						carac="3764";change=true;break;
					}else if(temp.equals("3884")){
						carac="3888";change=true;break;
					}else if(temp.equals("3875")){
						carac="3873";change=true;break;
					}else if(temp.equals("2652")){
						carac="2664";change=true;break;
					}else if(temp.equals("2965")){
						carac="2804";change=true;break;
					}else if(temp.equals("2941")){
						carac="2984";change=true;break;
					}else if(temp.equals("3783")){
						carac="3794";change=true;break;
					}else if(temp.equals("2944")){
						carac="2944";change=true;break;
					}else if(temp.equals("3722")){
						carac="3624";change=true;break;
					}else if(temp.equals("3534")){
						carac="3537";change=true;break;
					}else if(temp.equals("2623")){
						carac="2634";change=true;break;
					}else if(temp.equals("3732")){
						carac="3644";change=true;break;
					}else if(temp.equals("2627")){
						carac="2604";change=true;break;
					}else if(temp.equals("3833")){
						carac="3834";change=true;break;
					}else if(temp.equals("3822")){
						carac="3804";change=true;break;
					}else if(temp.equals("3461")){
						carac="3364";change=true;break;
					}else if(temp.equals("2293")){
						carac="2494";change=true;break;
					}else if(temp.equals("2362")){
						carac="2364";change=true;break;
					}else if(temp.equals("3488")){
						carac="3484";change=true;break;
					}else if(temp.equals("2322")){
						carac="2304";change=true;break;
					}else if(temp.equals("3717")){
						carac="3704";change=true;break;
					}
				
				}
				System.out.println("característica: "+carac); //numero	

				nuevotel="+54"+carac+telefono;
				System.out.println("nuevo numero: "+nuevotel); //numero	


				if(change==true){
					contact.setString(Contact.TEL,i,contactAtt,nuevotel);
					salida=salida+nuevotel+"\n";
				}else{
					salida=salida+"no cambia\n";
				}
				salida=salida+"atributos: "+contactAtt+"\n";
				salidaSI.setText(salida);


			}

			if(contact.isModified()) {
	     		   contact.commit();
	      		}
		

		} //while
       
	} catch (PIMException e) {

        }

	}//for

	try {
            cl.close();
        } catch (PIMException e) {
            // failed to close the list
        }



    }



} 
 
 

